/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * For more information, see <http://www.aljey.com>.
 */

//=Tabs
//=TabPanes

/**
 * Tab panel.
 *
 * @author Eugene Kuzmenko <mail@aljey.com>
 * @class {TabPanel}
 * @extends {Slot}
 */
Class.define('TabPanel', {

  extend: 'Slot',

  /**
   * The currently active tab index.
   *
   * @property {Number}
   */
  activeTab: 0,

  /** @constructs */
  init: function () {
    _super();
    this.items = [{
      type: 'Tabs',
      activeItem: this.activeTab
    }, {
      type: 'TabPanes',
      activeItem: this.activeTab
    }];
  },

  /**
   * Inserts tabs into a tab panel.
   *
   * @param {String[]|Object[]|Container[]} items
   * @param {Number} i
   * @returns {TabPanel}
   */
  insert: function (items, i) {
    if (!items || !this.el) return this;
    _.isArray(items) || (items = [items]);
    var tabs = _.cloneDeep(items);
    _.each(tabs, function (item, index, items) {
      _.isString(item) && (items[index] = item = {type: item});
      delete item.type;
      delete item.items;
    }, this);
    this.first.items.insert(tabs, i);
    this.last.items.insert(items, i);
    return this;
  },

  /**
   * Removes tabs.
   *
   * @returns {TabPanel}
   */
  remove: function () {
    if (!this.el) return this;
    _.each(arguments, function (item) {
      var i = _.isNumber(item) ? item : this.last.items.indexOf(item);
      this.first.items.remove(i);
      this.last.items.remove(i);
    }, this);
    return this;
  },

  /**
   * Switches between tabs.
   *
   * @param {Number} val
   * @returns {TabPanel}
   */
  setActiveItem: _static.Container.setter(function (val) {
    this.first.setActiveItem(val);
  }, 'activeTab', 'number'),

  listeners: {
    beforeRender: function () {
      var me = this;
      me.insert(me.tabs);
      me.first.on('tabChange', function (i) {
        me.activeTab = i;
        me.last.items.setActiveItem(i);
      });
      me.first.on('close', function (i) {
        me.last.items.remove(i);
      });
    }
  }

});